package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.core.domain.Ztree;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.common.utils.StringUtils;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.product.domain.Category;
import com.xingkong.product.service.ICategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/category")
public class CategoryController extends BaseController {
    private String prefix = "product/category";

    @Autowired
    private ICategoryService categoryService;

    @RequiresPermissions("product:category:view")
    @GetMapping()
    public String category() {
        return prefix + "/category";
    }

    /**
     * 查询商品分类树列表
     */
    @RequiresPermissions("product:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Category> list(Category category) {
        List<Category> list = categoryService.list(new QueryWrapper<Category>(category));
        return list;
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("product:category:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Category category) {
        List<Category> list = categoryService.list(new QueryWrapper<>());
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增商品分类
     */
    @GetMapping(value = {"/add/{id}", "/add/"})
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("category", categoryService.getById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("product:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Category category) {
        //默认当前的父节点为虚拟的跟节点 0号节点。
        int parentLevel = 0;
        String parentPath = "";
        //如果存在父节点，则在父节点的基础上进行操作
        if(category.getParentId()!=null && category.getParentId()!=0){
            Category parent = categoryService.getById(category.getParentId());
            parentLevel = parent.getCategoryLevel();
            parentPath = parent.getNodePath();
        }
        category.setCategoryLevel(parentLevel+1);
        category.setNodePath(parentPath +"/"+category.getCategoryName());
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改商品分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Category category = categoryService.getById(id);
        String parentName = "顶级分类";
        Category parent = categoryService.getById(category.getParentId());
        if(parent!=null){
            parentName = parent.getCategoryName();
        }
        category.setParentName(parentName);
        mmap.put("category", category);

        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("product:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Category category) {
        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(categoryService.removeById(id));
    }

    /**
     * 选择商品分类树
     */
    @GetMapping(value = {"/selectCategoryTree/{id}", "/selectCategoryTree/"})
    public String selectCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("category", categoryService.getById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载商品分类树列表
     */
    @GetMapping("/treeData/{id}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable("id") Long id) {
        List<Ztree> ztrees = categoryService.selectCategoryTree(id);
        return ztrees;
    }
}
